package com.daoImpl;

import com.bean.Book;
import com.dao.BookDao;
import com.spring.ConfigurationTest;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.List;

public class BookDaoImpl implements BookDao {
    static ApplicationContext application = new AnnotationConfigApplicationContext(ConfigurationTest.class);

    @Autowired
//    @Resource(name="runner")
    private QueryRunner runner;

    @Override
    public List<Book> findAll() throws SQLException {
        String sql = "select * from tb_book";
        List<Book> books = runner.query("select * from tb_book", new BeanListHandler<>(Book.class));

        return books;
    }

 /*   public static void main(String[] args) throws SQLException {
        QueryRunner runner = application.getBean("runner", QueryRunner.class);
        BookDaoImpl dao = new BookDaoImpl();
        System.out.println(dao.runner + " " + runner.getDataSource());
        List<Book> books = runner.query("select * from tb_book", new BeanListHandler<>(Book.class));
        System.out.println(books);
    }*/
}
